<?php

function birthday($birthday) {
    $age = strtotime($birthday);
    if ($age === false) {
        return 0;
    }
    list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age));
    $now = strtotime("now");
    list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now));
    $age = $y2 - $y1;
    if ((int) ($m2 . $d2) < (int) ($m1 . $d1))
        $age -= 1;
    return $age;
}

/**
  判断星座
 */
function birthext($birth) {
    if (strstr($birth, '-') === false && strlen($birth) !== 8) {
        $birth = date("Y-m-d", $birth);
    }
    if (strlen($birth) === 8) {
        if (eregi('([0-9]{4})([0-9]{2})([0-9]{2})$', $birth, $bir))
            $birth = "{$bir[1]}-{$bir[2]}-{$bir[3]}";
    }
    if (strlen($birth) < 8) {
        return false;
    }
    $tmpstr = explode('-', $birth);
    if (count($tmpstr) !== 3) {
        return false;
    }
    $m = (int) $tmpstr[1];
    $d = (int) $tmpstr[2];
    $xzdict = array('摩羯', '水瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手');
    $zone = array(1222, 122, 222, 321, 421, 522, 622, 722, 822, 922, 1022, 1122, 1222);
    if ((100 * $m + $d) >= $zone[0] || (100 * $m + $d) < $zone[1]) {
        $i = 0;
    } else {
        for ($i = 1; $i < 12; $i++) {
            if ((100 * $m + $d) >= $zone[$i] && (100 * $m + $d) < $zone[$i + 1]) {
                break;
            }
        }
    }
    return $xzdict[$i] . '座';
}

//二维数组去掉重复值
function array_unique_fb($array) {
    $result = array();
    foreach ($array as $key => $value) {
        $has = false;
        foreach ($result as $val) {
            if ($val['id'] == $value['id']) {
                $has = true;
                break;
            }
        }
        if (!$has) {
            $result[] = $value;
        }
        return $result;
    }
}

function checkPhone($phone) {
    $preg_phone = '/^1[3456789]\d{9}$/ims';
    if (preg_match($preg_phone, $phone)) {
        return true;
    } else {
        return false;
    }
}

//生成证书水印
function watermarklogo($img) {
    $dst_path = $img;
    $src_path = './uploads/yibeidaanlogo.png';
    $save_path = $img;
    //创建图片的实例

    $dst = imagecreatefromstring(file_get_contents($dst_path));

//    foreach ($img as $val) {
//    $src_path = $save_path;
    $src = imagecreatefromstring(file_get_contents($src_path));
    //获取水印图片的宽高
    list($src_w, $src_h) = getimagesize($src_path);

    //将水印图片复制到目标图片上，最后个参数50是设置透明度，这里实现半透明效果
    imagecopymerge($dst, $src, 150, 150, 0, 0, $src_w, $src_h, 100);
    //如果水印图片本身带透明色，则使用imagecopy方法
//    imagecopy($dst, $src, 300, 300, 0, 0, $src_w, $src_h);
    //输出图片
    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
    switch ($dst_type) {
        case 1://GIF
            header('Content-Type: image/gif');
            imagegif($dst, $save_path);
            break;
        case 2://JPG
            header('Content-Type: image/jpeg');
            imagejpeg($dst, $save_path);
            break;
        case 3://PNG
            header('Content-Type: image/png');
            imagepng($dst, $save_path);
            break;
        default:
            break;
    }
    imagedestroy($dst);
    imagedestroy($src);
//    }
}
